<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <script>

        ajax_post(
            'http://192.168.58.110/php/post.php',
            {
                username : 'qq' ,
                password : '123'
            } ,
            res => {
                console.log(res);
            }
        )

        function ajax_post(path , data , cb) {
            // 处理参数
            let params = '' ;
            for(let key in data) {
                params += `${key}=${data[key]}&`;
            }
            // 最后会一个&
            params = params.substring(0 , params.length - 1) ;


            const xhr = new XMLHttpRequest() ;
            xhr.open('post' , path , true) ;
            // 设置请求头
            xhr.setRequestHeader('content-type' , 'application/x-www-form-urlencoded');
            // key=value&key=value
            xhr.send(params);
            xhr.onreadystatechange = function() {
                if(xhr.readyState === 4 && xhr.status === 200) {
                    const res = xhr.responseText ;
                    cb(res);
                }
            }
        }

    </script>
    
</body>
</html>